package com.bwie.mq;

import com.bwie.mapper.SmsMapper;
import com.bwie.pojo.po.Sms;
import com.bwie.pojo.po.User;
import com.bwie.pojo.vo.OrderParkVo;
import com.bwie.service.UserService;
import com.bwie.utils.SendMsgUtil;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
import org.springframework.amqp.utils.SerializationUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.Serializable;
import java.util.Date;


public class EwMonthConsumer implements MessageListener {
    @Autowired
    private UserService userService;
    @Autowired
    private SmsMapper smsMapper;
    @Override
    public void onMessage(Message message) {
        byte[] body = message.getBody();
        OrderParkVo orderParkVo = (OrderParkVo) SerializationUtils.deserialize(body);
        System.out.println(orderParkVo);

        Integer userId = orderParkVo.getUserId();
        User user = userService.getUserById(userId);
        String smsRemarks="尊敬的"+user.getUsername()+"先生/女士，您预定的车位已超出规定时间，额外费用"+orderParkVo.getEwMoney();
        String isOk = SendMsgUtil.sendSms(user.getUserPhone(), "3333");


        //解析发送结果，保存到消息发送记录表中。
        Sms sms = new Sms();
        sms.setSmsRemarks(smsRemarks);
        sms.setSmsTime(new Date());
        sms.setSmsPhone(user.getUserPhone());
        sms.setSmsResp(isOk);

        smsMapper.addSms1(sms);
    }
}
